#include "CampaignEditForm.h"
#include "ui_CampaignEditForm.h"
#include "DataViewDelegate.h"


#include <ReferenceItem.h>
#include <D20CampaignItem.h>



CampaignEditFormBase::CampaignEditFormBase(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CampaignEditForm)
{
    m_model = new D20CampaignModel(this);

    ui->setupUi(this);

    // add actions
    addAction(ui->actionDelete);
    addAction(ui->actionAdd_Item);
    addAction(ui->actionAdd_Reference);

    // setup tree view
    ui->treeView->setItemDelegate(new DataViewDelegate(this));
}



CampaignEditFormBase::~CampaignEditFormBase()
{
    delete ui;
}



void CampaignEditFormBase::updateData()
{
    on_lineEditName_editingFinished();
}


QString CampaignEditFormBase::message() const
{
    if (!root()) return tr("Invalid Campaign!");
    return QString("%1 - %2").arg(root()->name()).arg(root()->filePath());
}




void CampaignEditFormBase::on_actionDelete_triggered()
{
    QAbstractItemModel* model = ui->treeView->model();
    if (!model) return;

    QModelIndex index = ui->treeView->currentIndex();
    if (!index.isValid()) return;

    model->removeRows(index.row(), 1, index.parent());
}



void CampaignEditFormBase::on_lineEditName_editingFinished()
{
    if (!root()) return;

    root()->setData(AbstractItem::NameData, ui->lineEditName->text());

}


void CampaignEditFormBase::initialize()
{
    if (!model()) return;
    model()->setRootItem(root());

    // setup line edit
    ui->lineEditName->setText(root()->name());

    ui->treeView->setModel(m_model);
    ui->treeView->header()->setResizeMode(QHeaderView::ResizeToContents);
    ui->treeView->hideColumn(AbstractItem::CategoryData);
    ui->treeView->hideColumn(AbstractItem::IdData);
}


void CampaignEditFormBase::onActionTriggered(QAction* action)
{
    if (!model()) return;

    QVariant data = action->data();
    if (!data.isValid()) return;

    int group = action->data().toInt();
    ui->treeView->header()->setResizeMode(QHeaderView::ResizeToContents);

    QModelIndex parent = model()->groupIndex(group);


    int row = model()->addItem(parent);
    if (row < 0) return;

    QModelIndex index = m_model->index(row, AbstractItem::NameData, parent);

    if (!index.isValid()) return;
    model()->setData(index, QString("%1 %2").arg(tr("New")).arg(model()->groupName(group)));
    ui->treeView->setCurrentIndex(index);
    ui->treeView->edit(index);
}



void CampaignEditFormBase::on_actionAdd_Reference_triggered()
{
    if (!model()) return;

    QModelIndex parent = ui->treeView->currentIndex();
    int row = model()->addReference(parent);

    QModelIndex index = model()->index(row, ReferenceItem::ReferenceData, parent);
    if (!index.isValid()) return;

    ui->treeView->setCurrentIndex(index);
    ui->treeView->edit(index);
}

void CampaignEditFormBase::on_actionAdd_Item_triggered()
{
    if (!model()) return;

    QModelIndex parent = ui->treeView->currentIndex();
    int row = model()->addItem(parent);

    QModelIndex index = model()->index(row, AbstractItem::NameData, parent);
    if (!index.isValid()) return;

    ui->treeView->setCurrentIndex(index);
    ui->treeView->edit(index);
}


